<template>
  <div id="app">
    <div class="container">
      <div class="close"><span class="iconfont iconicon-test"></span></div>
      <div class="logo"><span class="iconfont iconnew"></span></div>
      <div class="inputs">
        <hemaInput
          ref="username"
          v-model="user.username"
          placeholder="手机号"
          :reg="
            /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
          "
          type="text"
        ></hemaInput>
        <hemaInput
          ref="password"
          v-model="user.password"
          placeholder="密码"
          :reg="/^.{3,14}$/"
          type="password"
        ></hemaInput>
      </div>
      <van-divider content-position="right"
        ><p class="tips">
          没有账号？
          <a href="#/singIn" class="">去注册</a>
        </p></van-divider
      >
      <van-button round type="info" size="large" color="#d31e0d" @click="btnFn"
        ><van-icon name="star-o" />登录</van-button
      >
    </div>
  </div>
</template>

<script>
import hemaInput from "@/components/hemaInput.vue";
import { logIn } from "@/apis/get.js";
export default {
  data() {
    return {
      user: {
        username: "17673849042",
        password: "",
      },
    };
  },
  components: {
    hemaInput,
  },
  methods: {
    btnFn() {
      // console.log(this.$refs);
      let { username, password } = this.$refs;
      if (!username.flag || !password.flag) {
        this.$toast.fail("信息有误");
        return;
      } else {
        logIn(this.user).then((res) => {
          // console.log(res);
          this.$toast({
            message: res.data.message,
          });
          if (res.data.message == "登录成功") {
            // console.log(res.data.data.token);
            localStorage.setItem("token", res.data.data.token);
            localStorage.setItem("id", res.data.data.user.id);
            // 判定进入首页还是返回原页面
            // console.log(this.$route.hash.split("#")[1]);
            if (this.$route.hash) {
              // console.log(this.$router.hash.split("#")[1]);
              this.$router.push({
                path: this.$route.hash.split("#")[1],
              });
            } else {
              this.$router.push({
                path: "/personal",
              });
            }
          }
        });
      }
    },
  },
};
</script>

<style lang="less">
.container {
  padding: 20px;
}
.close {
  span {
    font-size: 27 / 360 * 100vw;
  }
}
.logo {
  display: flex;
  justify-content: center;

  span {
    display: block;
    font-size: 126 / 360 * 100vw;
    color: #d81e06;
  }
}
.inputs {
  input {
    margin-bottom: 20px;
  }
}
.tips {
  text-align: right;
  margin-bottom: 20px;
  a {
    color: #3385ff;
  }
}
</style>